package controller;

import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import view.overlay.ScrollingTextOverlay;
import view.overlay.ScrollingTextOverlay.OnScrollFinishListener;

public class IntroController extends Controller {
	
	private MainMenuController mMMC;
	private ScrollingTextOverlay mScroller;
	
	private GameControl mSkipControl;
	
	private File mAudioFile;
	
	public IntroController(ScrollingTextOverlay sto, MainMenuController mmc) {
		mScroller = sto;
		mMMC = mmc;
		
		
		
		// create and link the controls to an action
		createControls();

	}
	
	public void start() {
		mMMC.hideMainMenu();
		
		
		// listen for the scroller to be done
		mScroller.setOnScrollFinishListener(new OnScrollFinishListener() {

			@Override
			public void onFinish() {
				showMainMenu();
				
			}
			
		});
		

		// start
		mScroller.loadTextFromFile(new File("res/other/story2.txt"));
		mScroller.start();
		

		MidiPlayer mPlayer = MidiPlayer.getInstance();
		mPlayer.playMidi(mAudioFile);
		
}

	
	private void createControls() {
		mSkipControl = new GameControl(KeyEvent.VK_ESCAPE, "SKIP_INTRO", "Skips the intro sequence.") {

			@Override
			protected void execute() {
				showMainMenu();
				mSkipControl.setEnabled(false);
			}
			
		};
		mSkipControl.setReassignable(false);
	}

	@Override
	public List<GameControl> getKeyControls() {
		List<GameControl> ctrls = new ArrayList<GameControl>();
		
		ctrls.add(mSkipControl);
		
		return (ctrls);
	}
	
	private void showMainMenu() {
		// hide the scroller and activate main menu
		mScroller.setVisible(false);
		mScroller.stop();
		
		mMMC.showMainMenu();
	}
	
	public void setAudioFile(File audioFile) {
		mAudioFile = audioFile;
	}

	@Override
	public void reset() {
		mSkipControl.setEnabled(true);
		mScroller.reset();
		mScroller.setVisible(true);
		start();
	}
}
